package org.example.system.controller;

import org.example.system.entity.User;
import org.example.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginData) {
        String username = loginData.get("username");
        String password = loginData.get("password");
        Map<String, Object> result = new HashMap<>();
        User user = userService.findByUsername(username);
        if (user != null && user.getPassword().equals(password)) {
            result.put("success", true);
            result.put("message", "登录成功");
            // 可根据需要返回用户信息
        } else {
            result.put("success", false);
            result.put("message", "账号或密码错误");
        }
        return result;
    }

    @GetMapping("/login")
    public Map<String, Object> loginGet() {
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "请使用POST方式提交登录信息");
        return result;
    }

    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody Map<String, String> registerData) {
        String username = registerData.get("username");
        String password = registerData.get("password");
        String email = registerData.get("email");
        String phone = registerData.get("phone");
        Map<String, Object> result = new HashMap<>();
        if (username == null || username.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            result.put("success", false);
            result.put("message", "账号和密码不能为空");
            return result;
        }
        User exist = userService.findByUsername(username);
        if (exist != null) {
            result.put("success", false);
            result.put("message", "账号已存在");
            return result;
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        int row = userService.add(user);
        if (row > 0) {
            result.put("success", true);
            result.put("message", "注册成功");
        } else {
            result.put("success", false);
            result.put("message", "注册失败，请重试");
        }
        return result;
    }

    @GetMapping("/byName")
    public User getByName(@RequestParam String username) {
        return userService.findByUsername(username);
    }

    @PostMapping
    public Map<String, Object> add(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        int row = userService.add(user);
        result.put("success", row > 0);
        result.put("message", row > 0 ? "添加成功" : "添加失败");
        return result;
    }

    @GetMapping
    public Object getAll() {
        return userService.findAll();
    }

    @PutMapping
    public Map<String, Object> update(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        int row = userService.update(user);
        result.put("success", row > 0);
        result.put("message", row > 0 ? "修改成功" : "修改失败");
        return result;
    }

    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            int row = userService.delete(id);
            result.put("success", row > 0);
            result.put("message", row > 0 ? "删除成功" : "删除失败，id不存在或SQL错误");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "后端异常: " + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/{id}")
    public User getById(@PathVariable Long id) {
        return userService.findById(id);
    }
} 